// 引入axios
import axios from "axios";
import { ElMessage } from "element-plus"
import { $go2 } from "../utils/routerUtil";
axios.defaults.baseURL = "http://43.139.213.82:8086/";
axios.defaults.timeout = 10000;

/**
 * 请求拦截器
 */
axios.interceptors.request.use(
    config => {
        config.headers['Content-Type'] = 'application/json;charset=utf-8';
        const token = window.localStorage.getItem("token");
        if (token) {
            config.headers.Authorization ="Bearer"+' '+ token;
        }
        return config;
    }, (err) => {
        return Promise.reject(err);
    }
);

/**
 * 响应拦截器
 */
axios.interceptors.response.use((response) => {
    if (response.status == 200) {
        return Promise.resolve(response);
    } else {
        return Promise.reject(response);
    }
}, 
   async (error) => {
        handleError(error);
        return Promise.reject(error.response);
    })

/**
 * get请求
 * @param url 
 * @param params 
 * @returns 
 */
export function get(url = "", params = {}) {
    return new Promise((resolve, reject) => {
        axios
            .get(url, params)
            .then((res) => {
                resolve(res.data);
            })
            .catch((err) => {
                reject(err.data);
            });
    });
}

/**
 * post请求
 * @param {请求路径} url
 * @param {请求参数} params
 * @returns
 */
export function post(url: string = "", params = {}): any {
    return new Promise((resolve, reject) => {
        axios
            .post(url, params)
            .then((res) => {
                resolve(res.data);
            })
            .catch((err) => {
                reject(err.data);
            });
    });
}

/**
 * delete请求
 * @param {请求路径} url
 * @param {请求参数} params
 * @returns
 */
export function deleteMethod(url: string = "", params = {}): any {
    return new Promise((resolve, reject) => {
        axios
            .delete(url, params)
            .then((res) => {
                resolve(res.data);
            })
            .catch((err) => {
                reject(err.data);
            });
    });
}

/**
 * put请求
 * @param {请求路径} url
 * @param {请求参数} params
 * @returns
 */
export function put(url: string = "", params = {}): any {
    return new Promise((resolve, reject) => {
        axios
            .put(url, params)
            .then((res) => {
                resolve(res.data);
            })
            .catch((err) => {
                reject(err.data);
            });
    });
}

/**
 * 处理请求错误
 * @param error 
 */
export const handleError=(error:any)=>{
    if (error && error.response) {
        switch (error.response.status) {
            case 400:
                error.message = "错误请求";
                break;
            case 403:
                error.message = "拒绝访问";
                break;
            case 404:
                error.message = "请求错误,未找到资源";
                break;
            case 405:
                error.message = "请求方法未允许";
                break;
            case 408:
                error.message = "请求超时";
                break;
            case 500:
                error.message = "服务器端出错";
                break;
            case 501:
                error.message = "网络未实现";
                break;
            case 502:
                error.message = "网络错误";
                break;
            case 503:
                error.message = "服务不可用";
                break;
            case 504:
                error.message = "网络超时";
                break;
            case 505:
                error.message = "http版本不支持";
                break;
            default:
                error.message = `连接错误${error.response.status}`;
        }
    } else {
        if (JSON.stringify(error).includes("timeout")) {
            ElMessage.error("服务端响应超时,请刷新当前页！");
            $go2("/");
        }
        error.message("连接服务器失败");
    }
    ElMessage.error(error.message);
}

export default axios;